import axios from 'axios'
import { Toast, Dialog } from "vant";
import { getLocal } from "./localStorage";
import router from "../router";
let Base_URL//192.168.0.52   8.218.65.212 http://8.218.65.212:8080
// http://121.40.225.171
// http://192.168.0.119:8080 bendi
Base_URL = process.env.NODE_ENV === 'production' ? 'http://test.kuanqing.art' : 'http://test.kuanqing.art'
const service = axios.create({
    baseURL: Base_URL,
})
service.interceptors.request.use(
    config => {
        let token = localStorage.getItem("token");
        if (token != null) {
            config.headers['Authorization'] = token;
        }
        config.data = config.data
        return config
    },
    error => {
        return Promise.reject(error)
    }
)
service.interceptors.response.use(
    response => {
        Toast.clear()
        if (response.data.status == 401) {
            Toast('账号已过期 请重新登录')
            localStorage.clear()
        }
        if (response.status !== 200) {
            Toast(response.message)
            // Dialog.alert({
            //   title: '网络错误'
            // }).then(() => {
            // })
        }
        if (response.status == 404) {
            // router.push('/login')
        }
        else {
            if (!response.data.success) {
                Toast(response.data.message)
            }
            return response
        }
    }, error => {
        Toast(error.message)
        // Dialog.alert({
        //   title: '网络错误'
        // }).then(() => {
        // })
        return Promise.reject(error)
    }
)


function setConsole(data) {
    if (process.env.NODE_ENV !== 'production') {
        console.log(data)
    }
}

export default service